package com.airsystem.pos.bengkel.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.ForeignKey;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */

@Entity
@Table(name = "tbl_detail_sparepart")
public class DetailSparepart implements Serializable {

	@Transient
	public static final String ALIAS = "detail_sparepart";

	@Transient
	private int choice;

	@Id
	private String sub_merk;

	@ManyToOne
	@ForeignKey(name = "FK_SPAREPART")
	private Sparepart sparepart;

	@ManyToOne
	@JoinColumn(name = "jenis_sparepart", nullable = false)
	@ForeignKey(name = "FK_JENIS_SPAREPART")
	private JenisSparepart jenis_sparepart;

	@Id
	@ManyToOne
	@JoinColumn(name = "detail_kendaraan", nullable = false)
	@ForeignKey(name = "FK_SPAREPART_KENDARAAN")
	private DetailKendaraan detail_kendaraan;

	@OneToMany(mappedBy = "detail_sparepart", fetch = FetchType.LAZY)
	private Set<DetailPesanSparepart> detail_pesan_sparepart = new HashSet<DetailPesanSparepart>(0);

	@OneToMany(mappedBy = "detail_sparepart", fetch = FetchType.LAZY)
	private Set<DetailOrderLayanan> detail_order_layanan = new HashSet<DetailOrderLayanan>(0);

	private Long stok;

	private BigDecimal harga_satuan;

	static final String TOSTRING = "%s - %s [Sisa Stok: %s] [Harga Satuan: %s]";

	public String toString() {
		if (sub_merk == null) {
			return "-- PILIH --";
		}

		return String.format(TOSTRING, sparepart.getMerk(), sub_merk, stok.intValue(), harga_satuan.toBigInteger().intValue());
	}

	public String getSearchColumn() {
		StringBuffer sb = new StringBuffer();

		switch (choice) {
		case 1:
			return sb.append(DetailSparepart.ALIAS).append(".sparepart.merk").toString();
		case 2:
			return sb.append(DetailSparepart.ALIAS).append(".sub_merk").toString();
		case 3:
			return sb.append(DetailKendaraan.ALIAS).append(".kendaraan.merk").toString();
		case 4:
			return sb.append(DetailKendaraan.ALIAS).append(".sub_merk").toString();
		case 5:
			return sb.append(JenisSparepart.ALIAS).append(".nama").toString();
		case 6:
			return sb.append(DetailSparepart.ALIAS).append(".harga_satuan").toString();
		case 7:
			return sb.append(DetailSparepart.ALIAS).append(".stok").toString();
		case 99:
			return "USE_CRITERIA";

		default:
			return sb.append(DetailSparepart.ALIAS).append(".sparepart.merk").toString();
		}
	}

	public String getSearchKeyword() {
		switch (choice) {
		case 1:
			return getSparepart().getMerk();
		case 2:
			return getSub_merk();
		case 3:
			return getDetail_kendaraan().getKendaraan().getMerk();
		case 4:
			return getDetail_kendaraan().getSub_merk();
		case 5:
			return getJenis_sparepart().getNama();
		case 6:
			return getHarga_satuan().toString();
		case 7:
			return getStok_s();
		case 99:
			return "USE_CRITERIA";

		default:
			return "";
		}
	}

	public int getChoice() {
		return choice;
	}

	public void setChoice(int choice) {
		this.choice = choice;
	}

	public String getSub_merk() {
		return sub_merk;
	}

	public void setSub_merk(String sub_merk) {
		this.sub_merk = sub_merk;
	}

	public Sparepart getSparepart() {
		return sparepart;
	}

	public void setSparepart(Sparepart sparepart) {
		this.sparepart = sparepart;
	}

	public JenisSparepart getJenis_sparepart() {
		return jenis_sparepart;
	}

	public void setJenis_sparepart(JenisSparepart jenis_sparepart) {
		this.jenis_sparepart = jenis_sparepart;
	}

	public DetailKendaraan getDetail_kendaraan() {
		return detail_kendaraan;
	}

	public void setDetail_kendaraan(DetailKendaraan detail_kendaraan) {
		this.detail_kendaraan = detail_kendaraan;
	}

	public Set<DetailPesanSparepart> getDetail_pesan_sparepart() {
		return detail_pesan_sparepart;
	}

	public void setDetail_pesan_sparepart(Set<DetailPesanSparepart> detail_pesan_sparepart) {
		this.detail_pesan_sparepart = detail_pesan_sparepart;
	}

	public Set<DetailOrderLayanan> getDetail_order_layanan() {
		return detail_order_layanan;
	}

	public void setDetail_order_layanan(Set<DetailOrderLayanan> detail_order_layanan) {
		this.detail_order_layanan = detail_order_layanan;
	}

	public String getStok_s() {
		return (stok == null) ? "0" : String.valueOf(stok);
	}

	public Long getStok() {
		return stok;
	}

	public void setStok(Long stok) {
		this.stok = stok;
	}

	public String getHargaSatuan_s() {
		return (harga_satuan == null) ? "0" : String.valueOf(harga_satuan.intValue());
	}

	public BigDecimal getHarga_satuan() {
		return harga_satuan;
	}

	public void setHarga_satuan(BigDecimal harga_satuan) {
		this.harga_satuan = harga_satuan;
	}

	public boolean equals(Object object) {
		if (object instanceof DetailSparepart == false) {
			return false;
		}

		if (this == object) {
			return true;
		}

		DetailSparepart that = (DetailSparepart) object;

		return new EqualsBuilder().append(this.getDetail_kendaraan(), that.getDetail_kendaraan()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(this.getDetail_kendaraan()).hashCode();
	}
}